package com.zpb.framework.runner;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zpb.framework.domain.entity.Article;
import com.zpb.framework.mapper.ArticleMapper;
import com.zpb.framework.service.ArticleService;
import com.zpb.framework.utils.NumConst;
import com.zpb.framework.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2025/9/17
 */
@Component
public class ViewCountRunner implements CommandLineRunner {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    ArticleService articleService;

    @Override
    public void run(String... args) throws Exception {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<Article>();
        queryWrapper.eq(Article::getDelFlag, NumConst.ZERO)
                .eq(Article::getStatus, NumConst.ZERO);
        List<Article> articles = articleMapper.selectList(queryWrapper);
        Map<String, Integer> viewCountMap = articles.stream()
                .collect(Collectors.toMap(article1 -> article1.getId().toString(), article -> {
                    return article.getViewCount().intValue();
                }));
        redisCache.setCacheMap("article:viewCount",viewCountMap);
    }
}
